/**
 * Copyright (c) 2015-2017, Henry Yang 杨勇 (gismail@foxmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mapfinal.server.auth.service.impl;

import java.util.List;

import com.jfinal.kit.Kv;
import com.lambkit.common.service.LambkitModelServiceImpl;
import com.lambkit.core.aop.AopKit;
import com.mapfinal.server.auth.AuthAccessService;
import com.mapfinal.server.auth.model.AuthAccess;
import com.mapfinal.server.auth.model.AuthRule;

/**
 * @author yangyong 
 * @website: www.lambkit.com
 * @email: gismail@foxmail.com
 * @date 2020-09-23
 * @version 1.0
 * @since 1.0
 */
public class AuthAccessServiceImpl extends LambkitModelServiceImpl<AuthAccess> implements AuthAccessService {
	
	private AuthAccess DAO = null;
	
	public AuthAccess dao() {
		if(DAO==null) {
			DAO = AopKit.singleton(AuthAccess.class);
		}
		return DAO;
	}
	
	public boolean hasRule(Long userid, Integer ruleid) {
//		List<Roles> roles = Roles.service().getRoles(userid);
//		for (Roles role : roles) {
//			if(hasRule(role.getId(), ruleid)) {
//				return true;
//			}
//		}
//		return false;
		AuthAccess record = dao().findFirst(dao().getSqlPara("mapfinal.hasRuleOfUserId", ruleid, userid));
		return record!=null ? true : false;
	}
	
	public boolean hasRule(Integer roleid, Integer ruleid) {
		if (roleid == 1) {
			return true;
		}
		AuthAccess record = dao().findFirst(dao().getSqlPara("mapfinal.hasRule", roleid, ruleid));
		return record!=null ? true : false;
	}

	@Override
	public boolean hasRule(Long userid, String permission) {
		// TODO Auto-generated method stub
		AuthRule record = AuthRule.service().dao().findFirst(dao().getSqlPara("mapfinal.getRuleOfUserIdAndRuleName", userid, permission));
		return record!=null ? true : false;
	}

	@Override
	public boolean hasAnyRules(Long userid, String permissions) {
		// TODO Auto-generated method stub
		permissions = permissions.startsWith(",") ? permissions.substring(1) : permissions;
		permissions = permissions.endsWith(",") ? permissions.substring(0, permissions.length()-1) : permissions;
		Kv data = Kv.by("userid", userid).set("names", permissions.split(","));
		List<AuthRule> record = AuthRule.service().dao().find(dao().getSqlPara("mapfinal.getRuleOfUserIdAndRuleNames", data));
		return record != null && record.size() > 0 ? true : false;
	}

	@Override
	public boolean hasAllRules(Long userid, String permissions) {
		// TODO Auto-generated method stub
		permissions = permissions.startsWith(",") ? permissions.substring(1) : permissions;
		permissions = permissions.endsWith(",") ? permissions.substring(0, permissions.length()-1) : permissions;
		Kv data = Kv.by("userid", userid).set("names", permissions.split(","));
		List<AuthRule> rules = AuthRule.service().dao().find(dao().getSqlPara("mapfinal.getRuleOfUserIdAndRuleNames", data));
		permissions = "," + permissions + ",";
		if(rules!=null && rules.size() > 0) {
			for (AuthRule rule : rules) {
				permissions.replace(","+rule.getName()+",", ",");
			}
			if(",".equals(permissions)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
}
